/*

    This file is part of Blue Violin.

    Blue Violin is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Blue Violin is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero Public License for more details.

    You should have received a copy of the GNU Affero Public License
    along with Blue Violin.  If not, see <http://www.gnu.org/licenses/>.

    Blue Violin  Copyright (C) 2008  Blue Violin, Inc. & Josh Watts <josh.watts@blueviolin.org>
	For more information, visit http://www.blueviolin.org/

    This program comes with ABSOLUTELY NO WARRANTY.

*/

function Profiles()
{
	this.m_oCurrent = {"name": "",
					   "url" : "",
					   "emailOnlyOnFail" : false};
}


Profiles.prototype.createProfile = function(sName, sDefaultUrl, bEmailOnlyOnFail)
{
	var oHttp = $http("POST", "scripts.php");
	
	oHttp.send("action=createprofile&name=" + encodeURIComponent(sName) + 
			   "&defaultUrl=" + encodeURIComponent(sDefaultUrl) +
			   "&emailOnlyOnFail=" + bEmailOnlyOnFail.toString());
};
											

Profiles.prototype.open = function(sGuid)
{
	var oHttp = $http("POST", "scripts.php");
	var sProfile = "";
	var oProfile = null;
	var dtTemp = null;

	try
	{
		oHttp.send("action=openprofile&guid=" + encodeURIComponent(sGuid));
		sProfile = oHttp.responseText;


		oProfile = sProfile.parseJSON();

		this.m_oCurrent.name = oProfile.name;
		this.m_oCurrent.url = oProfile.url;
		this.m_oCurrent.emailOnlyOnFail = (0 == oProfile.emailOnlyOnFail) ? false : true;

		$get("txtToolbarAddress").value = this.m_oCurrent.url;

		dtTemp = new Date(2015, 11, 31);
		document.cookie = "BLUEVIOLIN_PROFILE=" + sGuid + ";expires=" + dtTemp.toGMTString() + ";path=/";


		$get("spnMenuLoginProfile").innerHTML = oProfile.name;
	}
	catch (e)
	{
		// do something
	}
};


Profiles.prototype.edit = function(oEditProfile)
{
	var oHttp = null;

	try
	{
		oHttp = $http("POST", "scripts.php");
		oHttp.send("action=editprofile&guid=" + encodeURIComponent(oEditProfile.guid) +
				   "&name=" + encodeURIComponent(oEditProfile.name) + 
				   "&url=" + encodeURIComponent(oEditProfile.defaultUrl) + 
				   "&emailOnlyOnFail=" + oEditProfile.emailOnlyOnFail.toString());
		
	}
	catch (e)
	{
		// do something
	}


};


Profiles.prototype.init = function()
{
	var aCookies = document.cookie.split(";");
	var aTemp = null;
	var i = 0;

	for (i = 0;
		 i < aCookies.length;
		 ++i)
	{
		aTemp = aCookies[i].split("=");
		if ("BLUEVIOLIN_PROFILE" == aTemp[0].trim())
		{
			this.open(aTemp[1].trim());
		}
	}
};


Profiles.prototype.get_emailOnlyOnFail = function()
{
	return this.m_oCurrent.emailOnlyOnFail;
	
}
	
